/**
 * 时间转换 标准时间
 * @param  {[string]} dateTime [时间戳]
 * @param  {[type]} type     [需要转化的类型]
 * 0:yy-mm-dd
 * 1:yy-mm-dd h:min
 * 2:yy-mm
 * @return {[type]}          [转化后的时间]
 */


function formatNormal(dateTime, type) {
    var unixTimestamp = new Date(dateTime);
    var y = unixTimestamp.getFullYear();
    var m = unixTimestamp.getMonth() + 1;
    var d = unixTimestamp.getDate();
    var h = unixTimestamp.getHours();
    var min = unixTimestamp.getMinutes();
    if (m < 10) {
        m = "0" + m;
    }
    if (d < 10) {
        d = "0" + d;
    }
    if (h < 10) {
        h = "0" + h;
    }
    if (min < 10) {
        min = "0" + min;
    }

    var time0 = y + "-" + m + "-" + d;
    var time1 = y + "-" + m + "-" + d + " " + h + ":" + min;
    var time2 = y + "-" + m;


    if (type == 0) {
        return time0;
    }
    if (type == 1) {
        return time1;
    }
    if (type == 2) {
        return time2;
    }
}


window.onload = function() {
    //提示用户浏览器版本过低跟换升级浏览器
    document.getElementsByTagName("body")[0].insertAdjacentHTML("beforeend", `
    <!--[if lt IE 9]>
    <div style="position:absoulute;left:0;top:0;width:100%;height:50px;background:rgb(230,104,27);color:rgb(255,255,255);border-bottom:1px solid rgb(230,230,198);text-align:center;line-height:50px;font-size:12px;">
    您使用的浏览器版本过低，可能会影响到您浏览本网页，建议您升级您的浏览器。如果您切换到了兼容模式，建议更改为极速模式浏览！
    </div>
    <![endif]-->
    `);
}



//全局API
var globalAPI = {};

//获取url参数
/*
方法返回一个关联数组，如需重复使用，需要用局部变量保存
var Request = globalAPI.getRequest();
获取url中的data参数，Request["data"]就可以得到，
如果只使用一次，globalAPI.getRequest()["data"]
*/
globalAPI.getRequest = function() {
    var url = decodeURI(location.search);
    var theRequest = [];
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
        }
    } else {
        console.log("url中没有参数");
        return;
    }
    return theRequest;
}

//时间戳转日期
/*
globalAPI.getDate({n:6156161651,format:"YYYY-MM-DD hh:mm:ss"})
globalAPI.getDate({
    n:1635161616, //n为时间戳，如果没有返回当前日期
    format:"YYYY-MM-DD hh:mm:ss" //format为返回的日期格式，其中英文将会被替换成对应日期的数字，注意大小写 如果没有参数返回的日期格式为 2017-12-15
})
*/
globalAPI.getDate = function(josn) {
    if (!josn) {
        var josn = {};
    }
    if (josn.n) {
        var nowdate = new Date(josn.n);
    } else {
        var nowdate = new Date();
    }
    var nowyear = nowdate.getFullYear(); //年
    var nowmonth = nowdate.getMonth() + 1; //月
    (nowmonth < 10) && (nowmonth = "0" + nowmonth);
    var nowri = nowdate.getDate(); //日
    (nowri < 10) && (nowri = "0" + nowri);
    if (!josn.format) {
        return nowyear + "-" + nowmonth + "-" + nowri;
    }
    var nowhour = nowdate.getHours(); //小时
    (nowhour < 10) && (nowhour = "0" + nowhour);
    var nowmin = nowdate.getMinutes(); //分钟
    (nowmin < 10) && (nowmin = "0" + nowmin);
    var nowsecond = nowdate.getSeconds(); //秒
    (nowsecond < 10) && (nowsecond = "0" + nowsecond);
    return josn.format.replace(/YYYY/, nowyear).replace(/MM/, nowmonth).replace(/DD/, nowri).replace(/hh/, nowhour).replace(/mm/, nowmin).replace(/ss/, nowsecond);
}

//验证图片类型
//参数为字符串 文件后缀
globalAPI.testImgType = function(str) {
    var str = str.toLowerCase(); //文件后缀可能为大写，这里转为小写
    var type = { //合格的 图片格式
        "jpg": 1,
        "png": 1,
    }
    return !!(type[str]); //返回布尔值
}

//获得总页数
function getPageCount(total, pageSize) {
    if (parseInt(total) <= parseInt(pageSize)) {
        return 1;
    }
    return total % pageSize == 0 ? total / pageSize : parseInt(total / pageSize) + 1;
}

//设置cookie hours是过期时间 现在后台维持登录是5个小时
/*
删除cookie
    globalAPI.setCookie(key, "", -1);
*/
globalAPI.setCookie = function(key, value, hours) {
    var expire = "";
    if (hours != null) {
        expire = new Date((new Date()).getTime() + hours * 3600000);
        expire = "; expires=" + expire.toGMTString();
    }
    document.cookie = key + "=" + escape(value) + expire + ";path=/"; //全域名下可用
};
//globalAPI.writeCookie("myCookie", "myname", 5);
//读取cookie
globalAPI.readCookie = function(key) {
    var cookieValue = "";
    var search = key + "=";
    if (document.cookie.length > 0) {
        var offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            var end = document.cookie.indexOf(";", offset);
            if (end == -1)
                end = document.cookie.length;
            cookieValue = unescape(document.cookie.substring(offset, end))
        }
    }
    return cookieValue;
};
//console.log(globalAPI.readCookie("myCookie"));

function startLogin(){
    globalAPI.setCookie("redirectUrl", window.location.href, 1);
    window.location.href = urlParam.location() + "/index/login.html";
}